package org.example.hansp.reflection.reflecAccessMethod;

import org.junit.jupiter.api.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@SuppressWarnings({"all"})
public class ReflectAccessMethod {
    Class<Boss> bossClazz = Boss.class;
    Boss boss = bossClazz.newInstance();

    public ReflectAccessMethod() throws InstantiationException, IllegalAccessException {
    }

    @Test
    public void test() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        Method hi = bossClazz.getMethod("hi");
        hi.invoke(boss);
    }

    @Test
    public void test2() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method info = bossClazz.getDeclaredMethod("info", String.class, int.class);
        info.setAccessible(true);
        System.out.println(info.invoke(boss, "Lim", 19));
    }

    @Test
    public void test3() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Method say = bossClazz.getDeclaredMethod("say", String.class);
        say.setAccessible(true);
        //因为say方法是静态的，所以可以传入null
        say.invoke(null, "Niko");
    }
}

class Boss {
    private String name;
    private int age;

    public void hi() {
        System.out.println("hi");
    }

    private String info(String name, int age) {
        return "name=" + name + ", age=" + age;
    }

    private static void say(String name) {
        System.out.println("hell: " + name);
    }
}